<?php

/**
 * @author dr.emi
 * @copyright 2010
 */

function loginTrigger()
{
    $_this = & get_Instance();
    if($_this->session->userdata('isLogin') == '')
    {
        redirect('user/login');
    }
}

function ambil_bulan($bln) {
	switch($bln) {
		case 1 : $nama_bulan = "Januari" ; break;
		case 2 : $nama_bulan = "Februari" ; break;
		case 3 : $nama_bulan = "Maret" ; break;
		case 4 : $nama_bulan = "April" ; break;
		case 5 : $nama_bulan = "Mei" ; break;
		case 6 : $nama_bulan = "Juni" ; break;
		case 7 : $nama_bulan = "Juli" ; break;
		case 8 : $nama_bulan = "Agustus" ; break;
		case 9 : $nama_bulan = "September" ; break;
		case 10 : $nama_bulan = "Oktober" ; break;
		case 11 : $nama_bulan = "November" ; break;
		case 12 : $nama_bulan = "Desember" ; break;
	}
	return $nama_bulan ;
}

function tgl_indonesia($tgl) {
	$tanggal=substr($tgl,8,2);
	$bulan=ambil_bulan(substr($tgl,5,2));
	$tahun=substr($tgl,0,4);
	return $tanggal." ".$bulan." ".$tahun ;
}

//buat array tanggal
function create_array_date($start, $end){
   $range = array();
   if (is_string($start) === true) $start = strtotime($start);
   if (is_string($end) === true ) $end = strtotime($end);
   if ($start > $end) return create_array_date($end, $start);
   do {
      $range[] = date('Y-m-d', $start);
      $start = strtotime("+ 1 day", $start);
   }
   while($start <= $end);
   return $range;
}

function create_arr_bulan($mulai,$akhir){
	$arr_date=create_array_date($mulai,$akhir);
	if(isset($arr_date)){
	foreach($arr_date as $val){
		$tmp=explode("-",$val);
		$bulan=$tmp[0]."-".$tmp[1];
		$arr_bulan[]=$bulan;
	}
	foreach($arr_bulan as $val){
		$arr_hasil[$val]=0;
	}
	foreach($arr_bulan as $val){
		$arr_hasil[$val]++;
	}
	return $arr_hasil;
	}else{
		return array();
	}
}

function buat_arr_bulan($mulai,$akhir){
	$arr_date=create_array_date($mulai,$akhir);
	foreach($arr_date as $val){
		$tmp=explode("-",$val);
		$bulan=$tmp[0]."-".$tmp[1];
		$arr_bulan[]=$bulan;
	}
	foreach($arr_bulan as $val){
		$arr_hasil[$val]="";
	}
	//array_unique($arr_bulan);
	
	foreach($arr_hasil as $key=>$val){
		$arr_baru[]=$key;
	}
	//print_r($arr_baru);
	return $arr_baru;
	
}

function konvert_tgl($tgl) {
	$tanggal=substr($tgl,8,2);
	$bulan=substr($tgl,5,2);
	$tahun=substr($tgl,0,4);
	return $tanggal."-".$bulan."-".$tahun ;

}

function tgl_english($tgl){
	$tanggal=substr($tgl,0,2);
	$bulan=substr($tgl,3,2);
	$tahun=substr($tgl,6,4);
	return $tahun."-".$bulan."-".$tanggal ;
}

function get_kota(){
	$_this = & get_Instance();
	$sql="SELECT * from kota order by nama asc";
		//echo $sql;
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$cabang= $query->result_array();
	}else{
		$cabang= array();
	}
	foreach($cabang as $row){
		$list_cabang[$row['id']]=$row['nama'];
	}
	echo form_dropdown('kota',$list_cabang);
}
function cek_login(){
	$_this = & get_Instance();
	$login=$_this->session->userdata('id_pelanggan');
	if(! isset($login)||$login != true){
			$_this->load->view('login/login_pelanggan');	
	}else{
		$_this->load->view('login/login_menu');
	}
}
function CustomPassword($password)
{
    $_this = & get_Instance();
    return sha1($password.$_this->config->item('encryption_key'));
}

function get_table_fld($table){

	$_this = & get_Instance();
	$sql = "show columns from $table ";
	
	$res  = $_this->db->query($sql);
	$rows = $res->result();
	foreach($rows as $r){
		$fld[] = $r->Field;
	}
	$fld = implode(';',$fld);
	
	return ($fld);
	
	
}

function make_array_key($str){
	$ar = array();
	$key = explode(';',$str);
	foreach($key as $k){
		$t = array($k=>'');
		$ar = array_merge($ar,$t);
	}
	return $ar;
}

function post2data($str){
	$_this = & get_Instance();
	$key = explode(';',$str);
	foreach($key as $k){
		if($_this->input->post($k)=='' ) continue;
		$data[$k] = ltrim(rtrim($_this->input->post($k)));
	}
	return $data;
}

function store_data($table,& $data,$id){
	$_this = & get_Instance();
	$result=0;
	if($_this->input->post($id)==''){
		if($_this->db->insert($table,$data)) {
			//$data[$id] = mysql_insert_id();
			$result = mysql_insert_id();
		}		
	} else {
		$_this->db->where($id,$_this->input->post($id));
		if($_this->db->update($table,$data)) //update($table = '', $set = NULL, $where = NULL, $limit = NULL)
			$result = $_this->input->post($id);
	}
	return $result;
}

function delete_data($table,$key,$id) 
{
    $_this = & get_Instance();
    $_this->db->query("delete from ".$table." where ".$key." ='".$id."'");	
}

function getFieldValue($tbl='', $field='', $param='', $value='')
{
    $_this = & get_Instance();
	$sql = $_this->db->query("SELECT * FROM $tbl WHERE $param = '$value'");
    if($sql->num_rows() !=0)
	{
	   $r = $sql->row();
       return $r->$field;
    }
    else
    {
        return false;
    }
}

function getModuleAccessForm($uid = '')
{
    $_this = & get_Instance();
	$sql="SELECT * from module where id_kategori<>1 or id=30";
		//echo $sql;
	$query=$_this->db->query($sql);
    $per_column = 2;
    $count = 0;
    $return = '';
    foreach($query->result() as $row)
    {
        $count ++;
        $return .= '<div style="margin:0 10px 10px 0; float: left">';
        if($uid != '')
        {
             $sql = $_this->db->query("select * from user_access WHERE user_id = '".$uid."' AND access_id = '".$row->id."'");
             if($sql->num_rows() == 1)
             {
                 $return .= form_checkbox('module_id[]', $row->id, TRUE) . "<label class='check_label'>".$row->name."</label>";
             }
             else
             {
                 $return .= form_checkbox('module_id[]', $row->id) . "<label class='check_label'>".$row->name."</label>";
             }
        }
        else
        {
             $return .= form_checkbox('module_id[]', $row->id) . "<label class='check_label'>".$row->name."</label>";
        }
        $return .= '</div>';
        if($count % $per_column == 0) { 
			$return .= '<div style="clear:both; height: 5px;">&nbsp;</div>'; 
		}        
    }
    return $return;
}

function getCabangAcces($uid = ''){
    $_this = & get_Instance();
	$sql="SELECT * from cabang";
		//echo $sql;
	$query=$_this->db->query($sql);
    $per_column = 2;
    $count = 0;
    $return = '';
	$list=$query->result();
	//print_r($list);
    foreach($list as $row)
    {
        $count ++;
        $return .= '<div style="margin:0 10px 10px 0; float: left">';
        if($uid != '')
        {
             $sql = $_this->db->query("select * from user_cabang WHERE id_user = '".$uid."' AND id_cabang = '".$row->kd_cabang."'");
             if($sql->num_rows() == 1)
             {
                 $return .= form_checkbox('cabang_id[]', $row->kd_cabang, TRUE) . "<label class='check_label'>".$row->nama."</label>";
             }
             else
             {
                 $return .= form_checkbox('cabang_id[]', $row->kd_cabang) . "<label class='check_label'>".$row->nama."</label>";
             }
        }
        else
        {
             $return .= form_checkbox('cabang_id[]', $row->kd_cabang) . "<label class='check_label'>".$row->nama."</label>";
        }
        $return .= '</div>';
        if($count % $per_column == 0) { $return .= '<div style="clear:both; height: 5px;">&nbsp;</div>'; }        
    }
    return $return;
}

function getModuleAccess($userID=0)
{
    if($userID != 0)
    {
        $_this = & get_Instance();
	    $sql = $_this->db->query("select * from user_access where user_id = '".$userID."'");
	    if($sql->num_rows() != 0)
        {
			echo "banyak data ".$sql->num_rows();
            echo "<ul style='margin:10px 0 0 30px;padding:0'>";
            foreach($sql->result_array() as $row)
            {
                echo "<li style='margin:0 0 10px 0;padding:0'>".getFieldValue('module', 'name', 'id', $row['access_id'])."</li>";
            }
            echo "<ul>";               
        }        
    }
}

function getModuleLink()
{
    $_this = & get_Instance();
	$sql = $_this->db->query("SELECT a . * , b . *
				FROM user_access a
				INNER JOIN module b ON a.access_id = b.id  where user_id=".$_this->session->userdata('user_id')." ORDER BY b.name");
    if($sql->num_rows() !=0)
	{
	   foreach($sql->result() as $row)
       {
            echo anchor('admin/'.$row->permalink, $row->name) . ' | ';
       }
    }
    else
    {
        return false;
    }
}

function accessTrigger() {
	$_this = & get_Instance();
	if($_this->session->userdata('level')!=1) {
	    $modID = getFieldValue('module', 'id', 'permalink', $_this->uri->segment(2));   
        $sql = $_this->db->query("select * from user_access where user_id = '".$_this->session->userdata('user_id')."' AND access_id = '".$modID."' LIMIT 1");
	    
        //let's skip general access area to checked'
        $skipped_area = array('','update','delete','save','access_area.php');
        
        if($sql->num_rows() == 1 || in_array($_this->uri->segment(2), $skipped_area))
        {
            return true;
        }
        else
        {
            die('You don\'t have access to this area. Please contact your Super Administrator.');
        }
	} 	
}

function getModul($id=''){
	$_this = & get_Instance();
	$user=$_this->session->userdata('id');
	$level=$_this->session->userdata('level');
	if ($level=="Petugas"){
		$sql="select a.*,b.* from user_access a inner join module b on a.access_id=b.id where b.id_kategori=".$id." 
		and a.id_pengguna=".$user." order by urutan asc";
	}else{
		$sql="select * from module where id_kategori=".$id." order by urutan asc";
	}
	//echo $sql;
	//echo $level;
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		return $query->result_array();
	}else{
		return array();
	}
}

function tampil_kategori_modul(){
	$_this = & get_Instance();
	$sql="SELECT * from kategori_modul order by urutan asc";
		//echo $sql;
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$data= $query->result_array();		
		foreach($data as $hasil){
			echo "<li><a>".$hasil['nama']."<span class='fa arrow'></span></a>";
			$list_modul=getModul($hasil['id']);
			//print_r($list_modul);
			echo "<ul class='nav naf nav-second-level'>";
			foreach($list_modul as $list){
				echo "<li><a href='".site_url($list['permalink'])."'>".$list['name']."</a></li>";
			}
			echo "</ul></li>";
		}
	}
	//$data=$_this->tes->getData();
	
	//foreach($data as $list){
		
	//}
}
?>